-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday24 - part1.py
More file actions
73 lines (59 loc) · 1.71 KB
/
Copy pathday24 - part1.py
File metadata and controls
73 lines (59 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
file = open("input.txt")
lines = file.read().splitlines()
CARDS = [(0, 1), (0, -1), (1, 0), (-1, 0), (0, 0)]
board = [list(line) for line in lines]
start = (0, 0)
for y, c in enumerate(board[0]):
if c == ".":
start = (0, y)
break
end = (0, 0)
for y, c in enumerate(board[-1]):
if c == ".":
end = (len(board) - 1, y)
break
blizzards = []
walls = set()
for y, row in enumerate(board):
for x, cell in enumerate(row):
if cell in "><v^":
blizzards.append((cell, (y, x)))
elif cell == "#":
walls.add((y, x))
walls.add((-1, start[1]))
walls.add((end[0] + 1, end[1]))
def get_blizzards(board, blizz):
new_blizzards = []
for b in blizz:
pos = b[1]
if b[0] == ">":
pos = (pos[0], pos[1] + 1)
elif b[0] == "<":
pos = (pos[0], pos[1] - 1)
elif b[0] == "^":
pos = (pos[0] - 1, pos[1])
elif b[0] == "v":
pos = (pos[0] + 1, pos[1])
if pos in walls:
if b[0] == ">":
pos = (pos[0], 1)
elif b[0] == "<":
pos = (pos[0], len(board[0]) - 2)
elif b[0] == "^":
pos = (len(board) - 2, pos[1])
elif b[0] == "v":
pos = (1, pos[1])
new_blizzards.append((b[0], pos))
return new_blizzards
states = {start}
time = 0
while end not in states:
time += 1
new_states = set()
blizzards = get_blizzards(board, blizzards)
blizzard_set = set(b for _, b in blizzards)
for curr in states:
pot = {(curr[0] + dy, curr[1] + dx) for (dy, dx) in CARDS}
new_states |= pot - blizzard_set - walls
states = new_states
print(time)